package com.cro.prison.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cro.prison.dao.QueryCriteriaDao;
import com.cro.prison.entity.pojo.ErrorMessage;
import com.cro.prison.entity.vo.QueryCriteria;
import com.cro.prison.service.QueryCriteriaService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class QueryCriteriaServiceImpl extends ServiceImpl<QueryCriteriaDao, ErrorMessage> implements QueryCriteriaService {


  @Override
  public List<ErrorMessage> listByQuery(QueryCriteria queryCriteria) {
    QueryWrapper<ErrorMessage> queryWrapper = new QueryWrapper<>();
    if (queryCriteria.getIdentity() != null) {
      queryWrapper.eq("identity", queryCriteria.getIdentity());
    }
    if (queryCriteria.getType() != null) {
      queryWrapper.eq("type", queryCriteria.getType());
    }
    if (queryCriteria.getStatus() != null) {
      queryWrapper.eq("status", queryCriteria.getStatus());
    }
    if (!(StringUtils.isEmpty(queryCriteria.getStart())&& StringUtils.isEmpty(queryCriteria.getEnd()))) {
      queryWrapper.ge("create_time", queryCriteria.getStart())
              .le("create_time", queryCriteria.getEnd());
    }
    //departId


    List<ErrorMessage> errorMessages = this.baseMapper.selectList(queryWrapper);
    return errorMessages;
  }

  @Override
  public int selectByType(QueryCriteria queryCriteria) {
    QueryWrapper<ErrorMessage> queryWrapper = new QueryWrapper<>();
    if (!(StringUtils.isEmpty(queryCriteria.getStart())&& StringUtils.isEmpty(queryCriteria.getEnd()))) {
      queryWrapper.ge("create_time", queryCriteria.getStart())
              .le("create_time", queryCriteria.getEnd());
    }
    //queryWrapper.groupBy("type");
     queryWrapper.eq("type",queryCriteria.getType());
     Integer integer = this.baseMapper.selectCount(queryWrapper);
     return integer;

  }
}
